package com.itcast.myrpc.decoder;

import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.ByteToMessageDecoder;

import java.util.List;

/**
 * @program: bio_tcp
 * @description:
 * @author: zhanghz001
 * @create: 2021-02-22 17:38
 **/
public class ByteToIntegerDecoder extends ByteToMessageDecoder {
    @Override
    protected void decode(ChannelHandlerContext channelHandlerContext,
                          ByteBuf byteBuf, List<Object> list) throws Exception {
        //是否有4个字节,如果有才够int类型
        if (byteBuf.readableBytes() >= 4) {
            Integer msg = byteBuf.readInt();
            //读取到int类型数据,放到输出,完成数据类型的转换
            list.add(msg);
        }
    }
}
